/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including but not limited to the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to provide persons with access to the Software
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGE OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

import React, { useRef, useState, useEffect } from 'react';
import { BreakpointManager, TabBarComponent } from '@hadss/web_adaptive_layout_ui';
import { getBreakpointsAttr } from '../../utils/getBreakpoint';
import { getScopeId } from '../../utils/getScopeId';
import { TabBarProps } from '../../types/multiTabBar';

declare global {
  namespace JSX {
    interface IntrinsicElements {
      'tab-bar': React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement> & {
        value?: string | number;
        width?: string | number | object;
        height?: string | number | object;
        'unselected-color'?: string | object;
        'selected-color'?: string | object;
        'layout-mode'?: string | object;
        'vertical-align'?: string | object;
        'horizontal-align'?: string | object;
        'bg-color'?: string | object;
        'selected-bg-color'?: string | object;
        scope?: string;
        class?: string;
      }
    }
  }
}

export const TabBarReact: React.FC<TabBarProps> = (props) => {
  const scopeId = getScopeId();
  const tabBarRef = useRef<TabBarComponent>(null);
  const propsRef = useRef(props);
  const [width, setWidth] = useState(undefined);
  const [height, setHeight] = useState(undefined);
  const [unselectedColor, setUnselectedColor] = useState(undefined);
  const [selectedColor, setSelectedColor] = useState(undefined);
  const [layoutMode, setLayoutMode] = useState(undefined);
  const [verticalAlign, setVerticalAlign] = useState(undefined);
  const [horizontalAlign, setHorizontalAlign] = useState(undefined);
  const [bgColor, setBgColor] = useState(undefined);
  const [selectedBgColor, setSelectedBgColor] = useState(undefined);
  const onTabItemClick = useRef(props.onTabItemClick || (() => {}));
  const breakpointManager = useRef<BreakpointManager>(null);
  const unsubscribe = useRef<Function>(() => {});

  const getCurrentAttr = () => {
    if (propsRef.current.width !== undefined) {
      setWidth(getBreakpointsAttr(propsRef.current.width, breakpointManager.current));
    }
    if (propsRef.current.height !== undefined) {
      setHeight(getBreakpointsAttr(propsRef.current.height, breakpointManager.current));
    }
    if (propsRef.current.unselectedColor !== undefined) {
      setUnselectedColor(getBreakpointsAttr(propsRef.current.unselectedColor, breakpointManager.current));
    }
    if (propsRef.current.selectedColor !== undefined) {
      setSelectedColor(getBreakpointsAttr(propsRef.current.selectedColor, breakpointManager.current));
    }
    if (propsRef.current.layoutMode !== undefined) {
      setLayoutMode(getBreakpointsAttr(propsRef.current.layoutMode, breakpointManager.current));
    }
    if (propsRef.current.verticalAlign !== undefined) {
      setVerticalAlign(getBreakpointsAttr(propsRef.current.verticalAlign, breakpointManager.current));
    }
    if (propsRef.current.horizontalAlign !== undefined) {
      setHorizontalAlign(getBreakpointsAttr(propsRef.current.horizontalAlign, breakpointManager.current));
    }
    if (propsRef.current.bgColor !== undefined) {
      setBgColor(getBreakpointsAttr(propsRef.current.bgColor, breakpointManager.current));
    }
    if (propsRef.current.selectedBgColor !== undefined) {
      setSelectedBgColor(getBreakpointsAttr(propsRef.current.selectedBgColor, breakpointManager.current));
    }
  };

  useEffect(() => {
    propsRef.current = props;
    if (breakpointManager.current) {
      getCurrentAttr();
    }
  }, [props]);

  useEffect(() => {
    if (tabBarRef.current) {
      tabBarRef.current.registerEvents(onTabItemClick.current);
    }

    breakpointManager.current = new BreakpointManager();
    unsubscribe.current = breakpointManager.current.subscribeToBreakpoint(() => {
      getCurrentAttr();
    });

    return () => {
      unsubscribe.current();
      breakpointManager.current.destroy();
    }
  }, []);

  return (
    <tab-bar
      ref={tabBarRef}
      scope={scopeId}
      class={scopeId}
      value={props.defaultActiveKey}
      width={width}
      height={height}
      unselected-color={unselectedColor}
      selected-color={selectedColor}
      layout-mode={layoutMode}
      vertical-align={verticalAlign}
      horizontal-align={horizontalAlign}
      bg-color={bgColor}
      selected-bg-color={selectedBgColor}
      >
      {props.children}
    </tab-bar>
  )
};

TabBarReact.displayName = 'TabBarReact';